﻿package mortal.game.view.skill.panel.render
{
    import Message.Public.*;
    import com.gengine.core.frame.*;
    import com.gengine.resource.*;
    import com.mui.controls.*;
    import com.mui.core.*;
    import com.mui.display.*;
    import com.mui.utils.*;
    import extend.language.*;
    import flash.events.*;
    import flash.text.*;
    import mortal.common.*;
    import mortal.common.global.*;
    import mortal.common.swfPlayer.*;
    import mortal.common.swfPlayer.data.*;
    import mortal.common.text.*;
    import mortal.component.gconst.*;
    import mortal.game.cache.*;
    import mortal.game.events.*;
    import mortal.game.manager.*;
    import mortal.game.mvc.*;
    import mortal.game.resource.*;
    import mortal.game.resource.info.*;
    import mortal.game.resource.info.item.*;
    import mortal.game.view.common.*;
    import mortal.game.view.common.display.*;
    import mortal.game.view.common.item.*;
    import mortal.game.view.common.util.*;
    import mortal.game.view.skill.*;
    import mortal.mvc.core.*;

    public class SkillEquipItem extends GSprite
    {
        private var _bg:GImageBitmap;
        private var _bmpType:GBitmap;
        private var _txtName:GTextFiled;
        private var _curLv:BitmapNumberText;
        private var _maxLv:BitmapNumberText;
        private var _skillItem:SkillItem;
        private var _levelUpEffect:SWFPlayer;
        private var _btnLevelUp:GLoadingButton;
        private var _txtNeedLabel:GTextFiled;
        private var _txtBookName:GTextFiled;
        private var _line:ScaleBitmap;
        private var _txtHaveBook:GTextFiled;
        private var _bookItem:BaseItem;
        private var _needEquips:AutoLayoutTextContainer;
        private var _selected:Boolean = false;
        private var _selectedEffect:ScaleBitmap;
        private var _skillInfo:SkillInfo;
        public var index:int;
        private var _lastClickTime:int = -1111;
        private static var _loaded:Boolean = false;

        public function SkillEquipItem()
        {
            return;
        }// end function

        public function set selected(param1:Boolean) : void
        {
            this._selected = param1;
            this.updateSelectedView(this._selected);
            return;
        }// end function

        public function get selected() : Boolean
        {
            return this._selected;
        }// end function

        public function updateSelectedView(param1:Boolean = false) : void
        {
            if (param1)
            {
                if (this._selectedEffect == null)
                {
                    this._selectedEffect = UIFactory.bg(0, 0, 132, 372, this, "SkillItemSelectedBg");
                }
            }
            else if (this._selectedEffect != null)
            {
                this._selectedEffect.dispose(true);
                this._selectedEffect = null;
            }
            return;
        }// end function

        public function get skillItem() : SkillItem
        {
            return this._skillItem;
        }// end function

        public function get skillInfo() : SkillInfo
        {
            return this._skillInfo;
        }// end function

        public function set skillInfo(param1:SkillInfo) : void
        {
            var _loc_3:ItemInfo = null;
            var _loc_4:int = 0;
            var _loc_5:EquipmentSuitData = null;
            var _loc_6:int = 0;
            var _loc_7:ColorInfo = null;
            var _loc_8:String = null;
            var _loc_9:int = 0;
            var _loc_10:AttributeData = null;
            var _loc_11:GTextFormat = null;
            var _loc_12:GTextFiled = null;
            this._skillInfo = param1;
            this._skillItem.skillInfo = this._skillInfo;
            this._txtName.text = this._skillInfo.skillName;
            this._txtName.x = 67 - this._skillInfo.skillName.length * 7.5;
            var _loc_2:* = this._skillInfo.suitId;
            if (this._skillInfo.learned)
            {
                this._txtNeedLabel.text = Language.getString(21062);
                this.disposeNotActiveStatusUI();
                this.addActiveStatusUI();
                LoaderHelp.setBitmapdata(ImagesConst.equipSkillActiveBg + ".swf", this._bg);
                this._txtName.y = 28;
                this._skillItem.filters = [];
                this._skillItem.isDragAble = !this._skillInfo.isBeAttackSkill;
                if (this._skillInfo.isMaxLevel() || this._skillInfo.nextSkill == null)
                {
                    this._txtBookName.text = Language.getString(21063);
                    this._txtHaveBook.htmlText = "";
                    this._bookItem.dispose(true);
                    this._bookItem = null;
                }
                else if (this._skillInfo.nextSkill.needSkillBook > 0)
                {
                    _loc_3 = ItemConfig.instance.getConfig(this._skillInfo.nextSkill.needSkillBook);
                    this._txtBookName.text = _loc_3.name;
                    _loc_4 = Cache.instance.pack.backPackCache.getItemCountByItemCode(_loc_3.code);
                    this._txtHaveBook.htmlText = Language.getStringByParam(21064, _loc_4);
                    this._bookItem.itemCode = _loc_3.code;
                }
                else
                {
                    this._txtBookName.text = Language.getString(21065);
                    this._txtHaveBook.htmlText = "";
                    this._bookItem.dispose(true);
                    this._bookItem = null;
                }
            }
            else
            {
                this._txtNeedLabel.text = Language.getString(21066);
                this.disposeActiveStatusUI();
                this.addNotActiveStatusUI();
                LoaderHelp.setBitmapdata(ImagesConst.equipSkillUnableBg + ".swf", this._bg);
                this._txtName.y = 36;
                this._skillItem.isDragAble = false;
                this._skillItem.filters = [FilterConst.colorFilter];
                _loc_5 = EquipmentUtil.getEquipmentSuitData(_loc_2, Cache.instance.role.entityInfo.career, EColor._EColorGold);
                _loc_6 = this._skillInfo.tSkill.levelLimit;
                _loc_7 = ColorConfig.instance.getItemColor(_loc_5.color);
                _loc_8 = _loc_7.colorText;
                _loc_9 = 0;
                while (_loc_9 < _loc_5.parts.length)
                {
                    
                    _loc_10 = _loc_5.parts[_loc_9] as AttributeData;
                    _loc_11 = GlobalStyle.textFormatPutong.center();
                    _loc_11.color = _loc_7.intColor;
                    _loc_11.size = 12;
                    _loc_12 = this._needEquips.getTextByIndex(_loc_9);
                    if (_loc_12 == null)
                    {
                        _loc_12 = this._needEquips.addNewText(130, "", 12, 65280, TextFieldAutoSize.CENTER, 3, _loc_11);
                    }
                    _loc_12.text = _loc_6.toString() + "级" + _loc_8 + _loc_10.name;
                    if (_loc_10.isActive)
                    {
                        _loc_12.filters = [FilterConst.glowFilter];
                    }
                    else
                    {
                        _loc_12.filters = [FilterConst.colorFilter];
                    }
                    _loc_9++;
                }
                this._needEquips.split(_loc_9);
            }
            if (!this._skillInfo.learned)
            {
                this._btnLevelUp.styleName = ImagesConst.EquipSkillActiveBtn_upSkin;
            }
            else if (this._skillInfo.isMaxLevel())
            {
                this._btnLevelUp.styleName = ImagesConst.EquipSkillFullLvBtn_upSkin;
            }
            else
            {
                this._btnLevelUp.styleName = ImagesConst.EquipSkillLevelUpBtn_upSkin;
            }
            if (this._skillInfo.learnable() || this._skillInfo.upgradable())
            {
                this._btnLevelUp.enabled = true;
                this._btnLevelUp.mouseEnabled = true;
                this._btnLevelUp.filters = [FilterConst.guideFilter];
            }
            else
            {
                this._btnLevelUp.enabled = false;
                this._btnLevelUp.mouseEnabled = false;
                this._btnLevelUp.filters = [FilterConst.colorFilter];
            }
            this.updateUI();
            return;
        }// end function

        public function refreshRedPoint() : void
        {
            return;
        }// end function

        private function updateUI() : void
        {
            var _loc_1:int = 0;
            var _loc_2:int = 0;
            var _loc_3:int = 0;
            if (isDisposed || !_loaded || this._skillInfo == null)
            {
                return;
            }
            if (this._skillInfo.isBeAttackSkill)
            {
                this._bmpType.bitmapData = GlobalClass.getBitmapData(ImagesConst.SkillPanel_FZ);
            }
            else if (this._skillInfo.tSkill.rangeType == ESkillRangeType._ESkillRangeTypeSingle)
            {
                this._bmpType.bitmapData = GlobalClass.getBitmapData(ImagesConst.SkillPanel_DTGJ);
            }
            else
            {
                this._bmpType.bitmapData = GlobalClass.getBitmapData(ImagesConst.SkillPanel_QTGJ);
            }
            if (this._skillInfo.learned)
            {
                this._curLv.text = this._skillInfo.tSkill.skillLevel.toString();
                this._maxLv.text = "/" + this._skillInfo.maxLevel.toString();
                _loc_1 = this._curLv.text.length * 16;
                _loc_2 = this._maxLv.text.length * 16;
                _loc_3 = (130 - _loc_1 - _loc_2) / 2;
                this._curLv.x = _loc_3 + 2;
                this._maxLv.x = _loc_3 + _loc_1 - 1;
            }
            return;
        }// end function

        private function upgradeHandler(event:MouseEvent) : void
        {
            var _loc_2:* = FrameManager.flashFrame.getTime();
            if (_loc_2 - this._lastClickTime < 300)
            {
                return;
            }
            this._lastClickTime = _loc_2;
            Dispatcher.dispatchEvent(new DataEvent(EventName.SkillUpgradeReq, this._skillInfo.skillId));
            this.disposeLevelUpEffect();
            this._levelUpEffect = UICompomentPool.getUICompoment(SWFPlayer) as SWFPlayer;
            this._levelUpEffect.playNum = 1;
            this._levelUpEffect.timeRate = 1;
            this._levelUpEffect.move(268, 314);
            this._levelUpEffect.load("skillLearnEffect.swf", ModelType.NormalSwf, null);
            this._levelUpEffect.actionPlayCompleteHandler = this.disposeLevelUpEffect;
            this._levelUpEffect.play();
            this.addChildAt(this._levelUpEffect, 1);
            return;
        }// end function

        override protected function createDisposedChildrenImpl() : void
        {
            super.createDisposedChildrenImpl();
            this.mouseChildren = true;
            this.mouseEnabled = true;
            this._bg = UIFactory.gImageBitmap(null, 0, 0, this);
            pushUIToDisposeVec(UIFactory.bg(-25, 189, 148, 2, this, ImagesConst.SplitLine));
            this._skillItem = UICompomentPool.getUICompoment(SkillItem) as SkillItem;
            this._skillItem.toolTipMode = SkillItem.TooltipMode_ShowNext;
            this._skillItem.x = 35;
            this._skillItem.y = 86;
            this._skillItem.setSize(60, 60);
            this._skillItem.isDragAble = true;
            this._skillItem.isShowToolTip = true;
            this.addChild(this._skillItem);
            this._bmpType = UIFactory.bitmap(null, 39, 8, this);
            var _loc_1:* = GlobalStyle.textFormatBai.center();
            this._txtName = UIFactory.gTextField("", 4, 28, 130, 26, this, _loc_1);
            this._txtName.mouseEnabled = false;
            this._txtName.embedFonts = true;
            this._txtName.defaultTextFormat = GlobalStyle.windowTitle;
            this._txtName.selectable = false;
            this._txtName.filters = [FilterConst.titleGradientBevelFilter];
            this._txtName.autoSize = TextFieldAutoSize.LEFT;
            this._txtName.antiAliasType = AntiAliasType.ADVANCED;
            this._btnLevelUp = UIFactory.gLoadingButton(ImagesConst.EquipSkillActiveBtn_upSkin, 10, 154, 110, 33, this);
            LoaderHelp.addResCallBack(ResFileConst.skillPanel, this.skillPanelGot);
            this._btnLevelUp.enabled = false;
            this._btnLevelUp.mouseEnabled = false;
            this._btnLevelUp.configEventListener(MouseEvent.CLICK, this.upgradeHandler);
            _loc_1 = GlobalStyle.textFormatHong;
            _loc_1.size = 12;
            this._txtNeedLabel = UIFactory.gTextField(Language.getString(21062), 11, 192, 200, 24, this, _loc_1);
            return;
        }// end function

        private function skillPanelGot() : void
        {
            _loaded = true;
            this.updateUI();
            return;
        }// end function

        private function disposeLevelUpEffect() : void
        {
            if (this._levelUpEffect != null)
            {
                this._levelUpEffect.stop();
                this._levelUpEffect.dispose(true);
                this._levelUpEffect = null;
            }
            return;
        }// end function

        private function disposeActiveStatusUI() : void
        {
            if (this._curLv != null)
            {
                this._curLv.dispose(true);
                this._curLv = null;
            }
            if (this._maxLv != null)
            {
                this._maxLv.dispose(true);
                this._maxLv = null;
            }
            if (this._txtBookName != null)
            {
                this._txtBookName.dispose(true);
                this._txtBookName = null;
            }
            if (this._line != null)
            {
                this._line.dispose(true);
                this._line = null;
            }
            if (this._txtHaveBook != null)
            {
                this._txtHaveBook.dispose(true);
                this._txtHaveBook = null;
            }
            if (this._bookItem != null)
            {
                this._bookItem.dispose(true);
                this._bookItem = null;
            }
            return;
        }// end function

        private function disposeNotActiveStatusUI() : void
        {
            if (this._needEquips != null)
            {
                this._needEquips.dispose(true);
                this._needEquips = null;
            }
            return;
        }// end function

        private function addNotActiveStatusUI() : void
        {
            if (this._needEquips == null)
            {
                this._needEquips = UICompomentPool.getUICompoment(AutoLayoutTextContainer) as AutoLayoutTextContainer;
                this._needEquips.verticalGap = -6;
                this._needEquips.y = this._txtNeedLabel.y + 26;
                this.addChild(this._needEquips);
            }
            return;
        }// end function

        private function addActiveStatusUI() : void
        {
            var _loc_1:GTextFormat = null;
            if (this._txtBookName == null)
            {
                _loc_1 = GlobalStyle.textFormatAnjin;
                _loc_1.size = 12;
                this._txtBookName = UIFactory.gTextField("", this._txtNeedLabel.x, this._txtNeedLabel.y + 20, 130, 24, this, _loc_1);
            }
            if (this._line == null)
            {
                this._line = UIFactory.bg(-25, 234, 148, 2, this, ImagesConst.SplitLine);
            }
            if (this._txtHaveBook == null)
            {
                _loc_1 = GlobalStyle.textFormatBai;
                _loc_1.size = 12;
                this._txtHaveBook = UIFactory.gTextField("", this._txtNeedLabel.x, this._txtNeedLabel.y + 44, 130, 26, this, _loc_1);
            }
            if (this._bookItem == null)
            {
                this._bookItem = UIFactory.baseItem(45, 262, 40, 40, this);
                this._bookItem.setItemStyle(40, ImagesConst.shortcutItemBg, 2, 2);
                this._bookItem.isDragAble = false;
                this._bookItem.isDropAble = false;
                this._bookItem.isThrowAble = false;
            }
            if (this._curLv == null)
            {
                this._curLv = UIFactory.bitmapNumberText(56, 55, "SkillCurLevel.png", 16, 23, 0, this, 14);
                this._maxLv = UIFactory.bitmapNumberText(61, 55, "SkillMaxLevel.png", 16, 23, 0, this, 14);
            }
            return;
        }// end function

        override protected function disposeImpl(param1:Boolean = true) : void
        {
            super.disposeImpl(param1);
            this._skillInfo = null;
            this._bg.dispose(param1);
            this._bg = null;
            this._bmpType.dispose(param1);
            this._bmpType = null;
            this._txtName.dispose(param1);
            this._txtName = null;
            this._skillItem.dispose(param1);
            this._skillItem = null;
            this._btnLevelUp.dispose(param1);
            this._btnLevelUp = null;
            EffectManager.glowFilterUnReg(this);
            if (this._selectedEffect != null)
            {
                this._selectedEffect.dispose(param1);
                this._selectedEffect = null;
            }
            this._txtNeedLabel.dispose(param1);
            this._txtNeedLabel = null;
            this.disposeLevelUpEffect();
            this.disposeNotActiveStatusUI();
            this.disposeActiveStatusUI();
            return;
        }// end function

    }
}
